import {
  getTenantOrganization,
  getLogistics,
  getWareData,
  getCountry,
  getPlatforms
} from '@/api/public'
import cache from '@/plugins/cache'

const params = {
  pageNum: 1, pageSize: 999
}
const useCommonStore = defineStore(
  'common',
  {
    state: () => ({
      personnelOptions: cache.session.getJSON('personnel') || '', // 人员
      warehouseOptions: '', // 全部仓库
      overseaswarehouseOptions: '', // 海外仓库
      domesticwarehouseOptions: '', // 国内仓库
      logisticsOptions: '',  // 物流
      countryOptions: '', // 国家
      platformOptions: '', // 平台
    }),
    actions: {
      getOrganization() {
        return new Promise(async (resolve, reject) => {
          try {
            const res = await getTenantOrganization()
            cache.session.setJSON('personnel', res.data)
            this.personnelOptions = res.data
            resolve()
          } catch (e) {
            reject(e)
            throw Error(e)
          }
        })
      },
      getLogisticsList() {
        return new Promise(async (resolve, reject) => {
          try {
            const res = await getLogistics({
              pageSize: 1000
            })
            this.logisticsOptions = res.data
            resolve()
          } catch (e) {
            reject(e)
            throw Error(e)
          }
        })
      },
      getWarehouseList() {
        return new Promise(async (resolve, reject) => {
          try {
            const res = await getWareData()
            this.warehouseOptions = res.data
            resolve()
          } catch (e) {
            reject(e)
            throw Error(e)
          }
        })
      },
      getOverseasWarehouseList() {
        return new Promise(async (resolve, reject) => {
          try {
            const res = await getWareData({
              wms_attribute: [2,3,4]
            })
            this.warehouseOptions = res.data
            resolve()
          } catch (e) {
            reject(e)
            throw Error(e)
          }
        })
      },
      getDomesticWarehouseList() {
        return new Promise(async (resolve, reject) => {
          try {
            const res = await getWareData({
              wms_attribute: [1,5]
            })
            this.warehouseOptions = res.data
            resolve()
          } catch (e) {
            reject(e)
            throw Error(e)
          }
        })
      },
      getCountryList() {
        return new Promise(async (resolve, reject) => {
          try {
            const res = await getCountry()
            this.countryOptions = res.data
            resolve()
          } catch (e) {
            reject(e)
            throw Error(e)
          }
        })
      },
      getPlatformsList() {
        return new Promise(async (resolve, reject) => {
          try {
            const res = await getPlatforms()
            this.platformOptions = res.data
            resolve()
          } catch (e) {
            reject(e)
            throw Error(e)
          }
        })
      },
      
    }
  })

export default useCommonStore
